<template>
  <div class="shopcar-container">
    <div class="goods-list">
      <!-- 商品列表区域 -->
      <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getGoodsSelected[item.id]"
              @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"
            ></mt-switch>
            <img :src="item.img_url" alt />
            <div class="info">
              <h1>{{item.title}}</h1>
              <p>
                <span class="price">{{item.sell_price}}</span>
                <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jisuan">
          <div class="left">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span> 件，总价
              <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    <!-- <p>{{$store.getters.getGoodsSelected}}</p> -->
  </div>
</template>
<script>
import numbox from "../subcomponents/shopcar_number.vue";
export default {
  data() {
    return {
      goodslist: [], //购物车中所有商品的数据
      goodsJSON: [
        {
          id: 10,
          title: "小米(Mi)小米Note 16G双网通版",
          add_time: new Date().toLocaleString(),
          zhaiyao: "",
          click: "6",
          img_url:
            "//img.alicdn.com/tps/i1/O1CN013yL3ad29zFljHDY12_!!0-juitemmedia.jpg_560x370Q50s50.jpg_.webp",
          sell_price: 2799,
          market_price: 2999,
          stock_quantity: 60
        },
        {
          id: 11,
          title: "包邮进口Chabatree无铅玻璃木柄酱汁杯蜂蜜牛奶零食碗迷你蘸酱碗",
          add_time: new Date().toLocaleString(),
          zhaiyao: "",
          click: "3666",
          img_url:
            "//gw.alicdn.com/bao/uploaded/i2/858389760/TB2gzOPmstnpuFjSZFvXXbcTpXa_!!858389760.jpg_500x500q90.jpg_.webp",
          sell_price: 64.99,
          market_price: 123.99,
          stock_quantity: 3216
        },
        {
          id: 12,
          title: "真维斯男士潮牌韩版宽松直筒牛仔裤",
          add_time: new Date().toLocaleString(),
          zhaiyao: "",
          click: "520",
          img_url:
            "//img.alicdn.com/tps/i2/O1CN01abKVEV2AX94sLHgFm_!!0-juitemmedia.jpg_560x370Q50s50.jpg_.webp",
          sell_price: 120,
          market_price: 216,
          stock_quantity: 66
        }
      ]
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      //获取购物车商品列表
      var idArr = [];
      this.$store.state.car.forEach(item => idArr.push(item.id));
      if (idArr.length >= 0) {
        this.goodsJSON.forEach(element => {
          for (var i = 0; i < idArr.length; i++) {
            if (element.id == idArr[i]) {
              this.goodslist.push(element);
            }
          }
        });
      } else {
        return;
      }
      // console.log(idArr);
      // console.log(this.goodslist);
    },
    remove(id, index) {
      this.goodslist.splice(index, 1);
      this.$store.commit("removeFromCar", id);
    },
    selectedChanged(id, val) {
      // console.log("id:" + id + ",val:" + val);
      this.$store.commit("updateGoodsSelected", { id: id, selected: val });
    }
  },
  components: {
    numbox
  }
};
</script>
<style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }
    img {
      width: 60px;
      height: 60px;
    }
    h1 {
      font-size: 13px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .jisuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red {
      color: red;
      font-weight: bold;
      font-size: 16px;
    }
  }
}
</style>